package ds.factcom.view.console.client;

import ds.factcom.controller.client.ClientController;
import ds.factcom.exception.ClientHasInvoicesException;
import ds.factcom.exception.EmptyStringException;
import ds.factcom.exception.ExistDomainException;
import ds.factcom.exception.IsNotNumberException;
import ds.factcom.exception.IsNotStringException;
import ds.factcom.exception.NotCorrectDocumentNumberException;
import ds.factcom.exception.NotCorrectDocumentTypeException;
import ds.factcom.exception.NotExistDomainException;
import ds.factcom.view.console.BaseConsoleView;
import ds.factcom.view.validate.ValidatorClient;

/**
 * 
 * @author Javier De Luca, Maximiliano Herrera, Matias Samblancat
 *
 */
public class ClientConsoleView extends BaseConsoleView{

	private final ClientController clientController;
	private boolean stop = false;
	
	//Atributos
	private String stringAux;
	private Integer selection;
	private boolean desicion = true;
	private String name;
	private String documentType;
	private String documentNumber;
	private String address;
	

	public ClientConsoleView(ClientController clientController) {
		this.clientController = clientController;
		this.clientController.addObserver(this);
	}

	public void menuClient(){
		while (!stop){
			// Imprimo menu de opciones
			System.out.println("-------------------------------------------------------");
			System.out.println("MENU CLIENTES DE OPCIONES");
			System.out.println("-------------------------------------------------------");
			System.out.println("1.- Alta Cliente");
			System.out.println("2.- Modificar Cliente");
			System.out.println("3.- Baja Cliente");
			System.out.println("4.- Volver");
			System.out.println("-------------------------------------------------------");
			System.out.print("Opcion:");
			try {
				
				stringAux = getScanner().nextLine();
				ValidatorClient.correctSelection(stringAux);
				selection = Integer.valueOf(stringAux);

				switch (selection) {
				case 1:
					this.insertClient();
					break;
				case 2:
					this.modifyClient();
					break;
				case 3:
					this.deleteClient();
					break;
				case 4:
					this.back();
					break;
				default:
					System.out.println(stringAux+" es un valor equivocado");
				}
			} catch (EmptyStringException e) {
				showErrorMessage(e.getMessage());
			} catch (IsNotNumberException e) {
				showErrorMessage(e.getMessage());
			} catch (NumberFormatException e) {
				showErrorMessage(e.getMessage());
			}
		}
	}
	
	private void insertClient() {
		desicion = true;

		System.out.println("-------------------------------------------------------");
		System.out.println("ALTA CLIENTE");
		System.out.println("-------------------------------------------------------");
		
		while(this.desicion == true){
			try{
				System.out.println("Ingrese un Nombre: ");
				stringAux = getScanner().nextLine();
				ValidatorClient.isNotEmpty(stringAux);
				this.name = stringAux;
				
				System.out.println("Ingrese un tipo de documento "+getMessageDocumentType());
				stringAux = getScanner().nextLine();
				ValidatorClient.isCorrectDocumentType(stringAux);
				this.documentType = stringAux;
				
				System.out.println("Ingrese un Numero de documento: ");
				stringAux = getScanner().nextLine();
				ValidatorClient.isCorrectDocumentNumber(stringAux);
				this.documentNumber = stringAux;
				
				System.out.println("Ingrese un Domicilio: ");
				stringAux = getScanner().nextLine();
				this.address = stringAux;
				
				this.clientController.insert(this.name, 
						this.documentType, this.documentNumber, this.address);

			} catch (EmptyStringException e) {
				showErrorMessage(e.getMessage());
			} catch (IsNotStringException e) {
				showErrorMessage(e.getMessage());
			} catch (NotCorrectDocumentTypeException e) {
				showErrorMessage(e.getMessage());
			} catch (IsNotNumberException e) {
				showErrorMessage(e.getMessage());
			} catch (NotCorrectDocumentNumberException e) {
				showErrorMessage(e.getMessage());
			} catch (ExistDomainException e) {
				showErrorMessage(e.getMessage());
			}finally{
				this.getMessageContinue("Insertar");
			}
		}

		}

	private void deleteClient() {
		desicion = true;

		System.out.println("-------------------------------------------------------");
		System.out.println("BAJA CLIENTE");
		System.out.println("-------------------------------------------------------");

		while(desicion){
			
			try {
			
				System.out.println("Indique el tipo de documento "+getMessageDocumentType()+" del cliente a borrar:");
				stringAux = getScanner().nextLine();
				
				ValidatorClient.isCorrectDocumentType(stringAux);
				this.documentType = stringAux;
				System.out.println("Ingrese un Numero de documento: ");
				stringAux = getScanner().nextLine();
				ValidatorClient.isCorrectDocumentNumber(stringAux);
				this.documentNumber = stringAux;
				this.clientController.delete(this.documentType, this.documentNumber);
			} catch (EmptyStringException e) {
				showErrorMessage(e.getMessage());
			} catch (IsNotStringException e) {
				showErrorMessage(e.getMessage());
			} catch (NotCorrectDocumentTypeException e) {
				showErrorMessage(e.getMessage());
			} catch (IsNotNumberException e) {
				showErrorMessage(e.getMessage());
			} catch (NotCorrectDocumentNumberException e) {
				showErrorMessage(e.getMessage());
			} catch (NotExistDomainException e) {
				showErrorMessage(e.getMessage());
			} catch (ClientHasInvoicesException e) {
				showErrorMessage(e.getMessage());
			}finally{
				this.getMessageContinue("Eliminar");
			}
		
		}
	}

	private void modifyClient(){
		desicion = true;
		System.out.println("-------------------------------------------------------");
		System.out.println("MODIFICAR CLIENTE");
		System.out.println("-------------------------------------------------------");

		while(desicion){
			try{
				System.out.println("Indique el tipo de documento "+getMessageDocumentType()+" del cliente a modificar:");
				
				stringAux = getScanner().nextLine();
				ValidatorClient.isCorrectDocumentType(stringAux);
				String documentTypeAux = stringAux;
				
				System.out.println("Ingrese un Numero de documento: ");
				stringAux = getScanner().nextLine();
				ValidatorClient.isCorrectDocumentNumber(stringAux);
				String documentNumberAux = stringAux;
				
				this.clientController.findByDocument(documentTypeAux, documentNumberAux);
				
				System.out.println("Ingrese el nuevo Nombre: "+ 
						this.clientController.getName());
				this.stringAux = getScanner().nextLine();
				this.name = ValidatorClient.getNewData(this.clientController.getName(), this.stringAux);
				
				System.out.println("Ingrese el nuevo Domicilio: "+ 
						this.clientController.getAddress());
				this.stringAux = getScanner().nextLine();
				this.address = ValidatorClient.getNewData(this.clientController.getAddress(), this.stringAux);
				
				this.clientController.modify(name, address);
				
			}catch (EmptyStringException e) {
				showErrorMessage(e.getMessage());
			}catch (IsNotNumberException e) {
				showErrorMessage(e.getMessage());
			}catch (IsNotStringException e) {
				showErrorMessage(e.getMessage());
			}catch (NotCorrectDocumentTypeException e) {
				showErrorMessage(e.getMessage());
			} catch (NotCorrectDocumentNumberException e) {
				showErrorMessage(e.getMessage());
			} catch (NotExistDomainException e) {
				showErrorMessage(e.getMessage());
			} finally{
				this.getMessageContinue("modificar");
			}
		}
	}
	
	private void back() {
		this.stop = true;
	}
	
	private void getMessageContinue(String action){
		System.out.println("Desea "+action+" otro cliente S/N");
		this.stringAux = getScanner().nextLine();
		if("N".equalsIgnoreCase(this.stringAux)){
			this.desicion = false;
		}
	}
}
